import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.UIManager;

import java.awt.Color;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class FacultyServices extends JFrame {

	/**
	 * Create the panel.
	 */
	public FacultyServices(final String user) {
		getContentPane().setBackground(Color.ORANGE);
				getContentPane().setLayout(null);
				
				JButton btnAssignGrades = new JButton("Assign Grades");
				btnAssignGrades.setBounds(47, 75, 234, 29);
				getContentPane().add(btnAssignGrades);
				
				JButton btnUpdatePersonalInformation = new JButton("Update Personal Information");
				btnUpdatePersonalInformation.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					new FacPersonalInfo(user);
					}
				});
				btnUpdatePersonalInformation.setBounds(46, 121, 235, 29);
				getContentPane().add(btnUpdatePersonalInformation);
				
				JButton btnAssignTutors = new JButton("Assign Tutors");
				btnAssignTutors.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						new AssignTutor(user);
					}
				});
				btnAssignTutors.setBounds(47, 162, 234, 29);
				getContentPane().add(btnAssignTutors);
				
				JButton btnViewStudentPerformane = new JButton("View Student Performance");
				btnViewStudentPerformane.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try{
						Connection con = Connect.getConnection();
						Statement stat = con.createStatement();
						UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

					    UIManager.put("OptionPane.background", Color.ORANGE);
						String ct = JOptionPane.showInputDialog (getContentPane(),"<html><b>Enter Course:</b> *If no course is entered, report will be generated for your course* </html>" ); 
						String course_title=ct;
						ResultSet rs=stat.executeQuery("SELECT DISTINCT Course_Title FROM SECTION AS S WHERE S.Instructor_Username='" + user +"'");
						if (ct.equals("") ||ct.trim().length() ==0){
						while(rs.next()){
							course_title=rs.getString(1);
							}
						}
						//For >3
						boolean check =false;
						double three=0.0;
						String sthree="None";
						String gr="";
						double total=0;
						double cum=0;
						rs=stat.executeQuery("SELECT Grade FROM TUTORED_STUDENTS WHERE Course_Title='"+course_title+"' AND LOGS>3");
						while(rs.next()){
							check=true;
							gr=rs.getString(1);
							if(gr.equals("A")){
								cum+=4;
							}else if(gr.equals("B")){
								cum+=3;
							}else if(gr.equals("C")){
								cum+=2;
							}else if(gr.equals("D")){
								cum+=1;
							}
							total+=4;
						}
						if(check){
						three=(cum/total) *4;
						sthree=three+"";
						check=false;
						}
		                //For 1-3
						double two=0.0;
						String stwo="None";
						rs=stat.executeQuery("SELECT Grade FROM TUTORED_STUDENTS WHERE Course_Title='"+course_title+"' AND LOGS<3");
						while(rs.next()){
							check=true;
							cum=0;
							total=0;
							gr=rs.getString(1);
							if(gr.equals("A")){
								cum+=4;
							}else if(gr.equals("B")){
								cum+=3;
							}else if(gr.equals("C")){
								cum+=2;
							}else if(gr.equals("D")){
								cum+=1;
							}
							total+=4;
						}
						if(check){
							two=(cum/total) *4;
							check=false;
							stwo=two+"";
							}
						
						//No tutor
						double none=0;
						String snone="";
						rs=stat.executeQuery("SELECT Distinct R.Student_Username,R.Grade FROM REGISTERS AS R,SECTION AS S,TUTORED_STUDENTS AS T WHERE S.Course_Title='"+course_title +"' AND S.CRN = R.Section_CRN AND R.Student_Username Not In( SELECT Student_Username FROM TUTORED_STUDENTS WHERE Course_Title='" +course_title+"')");
						while(rs.next()){
							check=true;
							cum=0;
							total=0;
							gr=rs.getString(2);
							if(gr.equals("A")){
								cum+=4;
							}else if(gr.equals("B")){
								cum+=3;
							}else if(gr.equals("C")){
								cum+=2;
							}else if(gr.equals("D")){
								cum+=1;
							}
							total+=4;
						}
						if(check){
							none=(cum/total) *4;
							snone=none+"";
							check=false;
							}
						
					    Icon i=new ImageIcon(FacultyServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg"));
						JOptionPane.showMessageDialog(getContentPane(),"Course Title: "+course_title +"\n >3: " + sthree +"\n 1-3: " + stwo +"\n None: " + snone,"Effect of Tutoring on Student Performance",JOptionPane.INFORMATION_MESSAGE,i);
						}
						catch(Exception g){
							//JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
						}
					}
				});
				btnViewStudentPerformane.setBounds(47, 203, 234, 29);
				getContentPane().add(btnViewStudentPerformane);
				
				JLabel label = new JLabel("");
				label.setIcon(new ImageIcon(FacultyServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
				label.setBounds(283, 63, 234, 168);
				getContentPane().add(label);
				
				JButton btnLogout = new JButton("LOGOUT");
				btnLogout.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					dispose();
					new LogIn();
					}
				});
				btnLogout.setBounds(293, 243, 117, 29);
				getContentPane().add(btnLogout);
				setPreferredSize(new Dimension(600, 300));
				pack();
				setVisible(true);
				setTitle("Faculty Services");
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
